#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SLInit(SL* s)
{
	s->a = NULL;
	s->capacity = s->size = 0;
}

void SLCheckCapacity(SL* s)
{
	if (s->capacity == s->size)
	{
		int NewCapccity = s->capacity == 0 ? 4 : 2 * s->capacity;
		SLDataType* pa = (SLDataType*)realloc(s->a, NewCapccity * sizeof(SLDataType));
		if (pa == NULL)
		{
			perror("realloc->pa");
			return 1;
		}
		s->a = pa;
		s->capacity = NewCapccity;
	}
}

void SLDestroy(SL* s)
{

}

void SLPushBack(SL* s, SLDataType x)
{
	assert(s);
	SLCheckCapacity(s);
	s->a[s->size++] = x;
}

void SLPushFront(SL* s, SLDataType x)
{
	assert(s);
	SLCheckCapacity(s);
	for (int i = s->size;i > 0;i--)
	{
		s->a[i] = s->a[i - 1];
	}
	s->a[0] = x;
	s->size++;
}

void SLPeint(SL* s)
{
	for (int i = 0;i < s->size;i++)
	{
		printf("%d ",s->a[i]);
	}
	printf("\n");
}

void SLopBack(SL* s)
{
	assert(s);
	assert(s->size);
	s->size--;


}

void SLopFront(SL* s)
{
	assert(s);
	assert(s->size);
	for (int i = 0; i < s->size - 1; i++)
	{
		s->a[i] = s->a[i + 1];
	}
}


void SLInsert(SL* s, int pos, SLDataType x)
{
	assert(s);
	assert(pos >= 0&& pos <= s->size);
	SLCheckCapacity(s);

	for (int i = s->size ;i > pos;i--)
	{
		s->a[i] = s->a[i - 1];
	}
	s->a[pos] = x;
	s->size++;
}

void SLErase(SL* s, int pos)
{
	assert(s);
	assert(pos >= 0 && pos < s->size);

	for (int i = pos; i < s->size ; i++)
	{
		s->a[i] = s->a[i + 1];
	}
	s->size--;

}
